<template>
  <div class="app-container">
    <div class="app-flex">
      <el-button type="primary" @click="addUser">添加</el-button>
      <el-input
        placeholder="请输入姓名进行查询"
        v-model="searchByName"
        class="input-with-select"
        style="width: 400px; margin-left: 10px"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="clickSearchButton"
        ></el-button>
      </el-input>
    </div>

    <el-table
      v-loading="listLoading"
      :data="userPageData.records"
      border
      style="width: 100%"
    >
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleUpdateShow(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="uname" label="姓名" />
      <el-table-column prop="gender" label="性别" />
      <el-table-column prop="cardid" label="身份证" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="phone" label="手机号" />
      <el-table-column prop="houseid" label="住宅号" />
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, sizes, total, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="userPageData.size"
      :total="userPageData.total"
      @current-change="fetchData"
      @size-change="handleSizeChange"
    />
    <el-dialog title="基本信息" :visible.sync="dialogFormVisible">
      <el-form ref="UserForm" :rules="rules" :model="formData" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="formData.password" />
        </el-form-item>
        <el-form-item label="姓名" prop="uname">
          <el-input v-model="formData.uname" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="formData.gender" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="身份证号" prop="cardid">
          <el-input v-model="formData.cardid" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" />
        </el-form-item>
        <el-form-item label="住宅号" prop="houseid">
          <el-input type="number" v-model="formData.houseid" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveFormData">保存</el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getList, saveInfo, deleteUser } from "@/api/biz/base";
import { getTargetKeyValueFromSource } from "@/utils/easyUtils";

export default {
  data() {
    return {
      dialogFormVisible: false,
      listLoading: true,
      userPageData: {
        records: [],
        size: 10,
        total: 0,
        current: 1,
      },
      formData: {
        id: 0,
        username: "",
        password: "",
        uname: "",
        gender: "",
        cardid: "",
        address: "",
        phone: "",
        houseid: "",
      },
      searchByName: "",
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        uname: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
        ],
        gender: [
          {
            required: true,
            message: "请输入性别",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
          { min: 11, max: 11, message: "手机号格式错误", trigger: "blur" },
        ],
        cardid: [
          {
            required: true,
            message: "请输入身份证号",
            trigger: "blur",
          },
          { min: 18, max: 18, message: "身份证格式错误", trigger: "blur" },
        ],
        address: [
          {
            required: true,
            message: "请输入地址",
            trigger: "blur",
          },
        ],
        houseid: [
          {
            required: true,
            message: "请输入住宅号",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.fetchData(1);
  },
  methods: {
    clickSearchButton() {
      // console.log(this.searchByName);
      getList({})
        .then((response) => {
          var obj = response.data.records;
          for (const key in obj) {
            if (obj[key].uname === this.searchByName) {
              response.data.records = [obj[key]];
              this.userPageData = getTargetKeyValueFromSource(
                this.userPageData,
                response.data
              );
              return;
            }
          }
          this.$message.warning("查无此人！");
          return;
        })
        .catch((err) => {
          this.listLoading = false;
          console.log(err);
        });
    },
    handleSizeChange(val) {
      this.userPageData.size = val;
      this.fetchData(1);
    },
    fetchData(current) {
      this.userPageData.current = current;
      this.userPageData.records = [];
      this.listLoading = true;
      getList({}, this.userPageData)
        .then((response) => {
          this.userPageData = getTargetKeyValueFromSource(
            this.userPageData,
            response.data
          );
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
          console.log(err);
        });
    },
    handleUpdateShow(row) {
      this.formData = getTargetKeyValueFromSource(this.formData, row);
      this.dialogFormVisible = true;
    },
    handleDelete(row) {
      this.$confirm("确定删除用户？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteUser({ userId: row.id }).then((response) => {
            this.fetchData(1);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    saveFormData() {
      this.$refs['UserForm'].validate((valid) => {
        if (valid) {
          saveInfo(this.formData).then((response) => {
            this.dialogFormVisible = false;
            this.fetchData(1);
          });
        } else {
          this.$message.warning("输入数据错误！");
        }
      });
    },
    addUser() {
      this.formData = getTargetKeyValueFromSource(this.formData, {
        id: 0,
        username: "",
        password: "",
        uname: "",
        gender: "",
        cardid: "",
        address: "",
        phone: "",
        houseid: "",
      });
      this.dialogFormVisible = true;
    },
  },
};
</script>
<style scoped>
.app-flex {
  margin: 10px;
  display: flex;
}
</style>